Lua 您所在的位置:网站首页 lua 数学库 Lua

Lua

2023-06-25 14:10| 来源: 网络整理| 查看: 265

首页 >  lua >  Lua - 数学库

Lua - 数学库

我们在科学和工程计算中经常需要数学运算,我们可以使用标准 Lua 库数学来利用它。数学库中可用的函数列表如下表所示。

Sr.No. 库/方法和目的 1 [数学.abs (x)] 返回 x 的绝对值。 2 [math.acos (x)] 返回 x 的反余弦值(以弧度为单位)。 3 [数学.asin (x)] 返回 x 的反正弦(以弧度为单位)。 4 [math.atan (x)] 返回 x 的反正切(以弧度为单位)。 5 [math.atan2 (y, x)] 返回 y/x 的反正切(以弧度为单位),但使用两个参数的符号来查找结果的象限。 (它也能正确处理 x 为零的情况。) 6 [math.ceil (x)] 返回大于或等于 x 的最小整数。 7 [math.cos (x)] 返回 x 的余弦值(假定为弧度)。 8 [math.cosh (x)] 返回 x 的双曲余弦值。 9 [math.deg (x)] 返回以度为单位的角度 x(以弧度表示)。 10 [math.exp (x)] 返回值 e 幂 x。 11 [math.floor (x)] 返回小于或等于 x 的最大整数。 12 [math.fmod (x, y)] 返回 x 除以 y 的余数,将商向零舍入。 13 [math.freexp (x)] 返回 m 和 e 使得 x = m2e, e 是一个整数并且 m 的绝对值在 [0.5, 1) 范围内(或者当 x 为零时为零)。 14 [math.huge] 值 HUGE_VAL,大于或等于任何其他数值的值。 15 [math.ldexp (m, e)] 返回 m2e(e 应该是整数)。 16 [math.log (x)] 返回 x 的自然对数。 17 [math.log10 (x)] 返回 x 的以 10 为底的对数。 18 [math.max (x, ...)] 返回其参数中的最大值。 19 [math.min (x, ...)] 返回其参数中的最小值。 20 [math.modf (x)] 返回两个数字,x 的整数部分和 x 的小数部分。 21 [数学.pi] pi 的值。 22 [math.pow (x, y)] 返回 xy。 (您也可以使用表达式 x^y 来计算此值。) 23 [数学.rad (x)] 以弧度返回角度 x(以度为单位)。 24 [math.random ([m [, n]])] 此函数是 ANSI C 提供的简单伪随机生成器函数 rand 的接口。当不带参数调用时,返回范围 [0,1) 内的统一伪随机实数。当使用整数 m 调用时,math.random 返回范围 [1, m] 内的统一伪随机整数。当使用两个整数 m 和 n 调用时,math.random 返回 [m, n] 范围内的统一伪随机整数。 25 [math.randomseed (x)] 将 x 设置为伪随机生成器的“种子”:相同的种子产生相同的数字序列。 26 [数学.sin (x)] 返回 x 的正弦值(假定为弧度)。 27 [数学.sinh (x)] 返回 x 的双曲正弦值。 28 [数学.sqrt (x)] 返回 x 的平方根。 (您也可以使用表达式 x^0.5 来计算该值。) 29 [数学.tan (x)] 返回 x 的正切(假定为弧度)。 30 [math.tanh (x)] 返回 x 的双曲正切。 三角函数

使用三角函数的简单示例如下所示。

radianVal = math.rad(math.pi / 2) io.write(radianVal,"n") -- Sin value of 90(math.pi / 2) degrees io.write(string.format("%.1f ", math.sin(radianVal)),"n") -- Cos value of 90(math.pi / 2) degrees io.write(string.format("%.1f ", math.cos(radianVal)),"n") -- Tan value of 90(math.pi / 2) degrees io.write(string.format("%.1f ", math.tan(radianVal)),"n") -- Cosh value of 90(math.pi / 2) degrees io.write(string.format("%.1f ", math.cosh(radianVal)),"n") -- Pi Value in degrees io.write(math.deg(math.pi),"n")

当我们运行上面的程序时,我们会得到如下输出。

0.027415567780804 0.0 1.0 0.0 1.0 180 其他常用数学函数

下面是一个使用常用数学函数的简单示例。

-- Floor io.write("Floor of 10.5055 is ", math.floor(10.5055),"n") -- Ceil io.write("Ceil of 10.5055 is ", math.ceil(10.5055),"n") -- Square root io.write("Square root of 16 is ",math.sqrt(16),"n") -- Power io.write("10 power 2 is ",math.pow(10,2),"n") io.write("100 power 0.5 is ",math.pow(100,0.5),"n") -- Absolute io.write("Absolute value of -10 is ",math.abs(-10),"n") --Random math.randomseed(os.time()) io.write("Random number between 1 and 100 is ",math.random(),"n") --Random between 1 to 100 io.write("Random number between 1 and 100 is ",math.random(1,100),"n") --Max io.write("Maximum in the input array is ",math.max(1,100,101,99,999),"n") --Min io.write("Minimum in the input array is ",math.min(1,100,101,99,999),"n")

当我们运行上面的程序时,我们会得到如下输出。

Floor of 10.5055 is 10 Ceil of 10.5055 is 11 Square root of 16 is 4 10 power 2 is 100 100 power 0.5 is 10 Absolute value of -10 is 10 Random number between 1 and 100 is 0.22876674703207 Random number between 1 and 100 is 7 Maximum in the input array is 999 Minimum in the input array is 1

上面的例子只是一些常见的例子,我们可以根据自己的需要使用数学库,所以尽量使用所有的函数来熟悉。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有